import React, { useRef, FC } from 'react';
import ProTable, { ProColumns, ActionType } from '@/components/ProTable';
import styles from './index.less';
import moment from 'moment';
import { RunAlarmReturnType, RunAlarmParamsType } from '../../../../API';
interface Props {
  getHisAlarm: (params: RunAlarmParamsType & { [key: string]: any }) => Promise<any>;
  id?: number;
}

const LossCard: FC<Props> = ({ getHisAlarm, id }) => {
  const actionRef = useRef<ActionType>();

  const columns: ProColumns<RunAlarmReturnType>[] = [
    {
      title: '门名称',
      dataIndex: 'deviceName',
      colItemSpan: 4,
      formItemColConfig: { md: 6, lg: 6, xl: 5, xxl: 5 },
      align: 'center',
    },
    {
      title: '告警等级',
      dataIndex: 'alarmGrade',
      align: 'center',
      formItemColConfig: { md: 6, lg: 6, xl: 5, xxl: 5 },
      colItemSpan: 4,
      valueEnum: {
        1: '一级告警',
        2: '二级告警',
        3: '三级告警',
        4: '四级告警',
      },
    },
    {
      title: '时间区间',
      dataIndex: 'time',
      hideInTable: true,
      align: 'center',
      formItemColConfig: { md: 6, lg: 6, xl: 5, xxl: 5 },
      valueType: 'dateRange',
    },
    {
      title: '告警时间',
      dataIndex: 'startTime',
      hideInSearch: true,
      align: 'center',
      valueType: 'date',
    },
    {
      title: '告警信息',
      dataIndex: 'alarmMsg',
      hideInSearch: true,
      align: 'center',
    },
    {
      title: '状态',
      dataIndex: 'status',
      hideInSearch: true,
      align: 'center',
    },
  ];

  return (
    <>
      <ProTable<RunAlarmReturnType>
        columns={columns}
        request={async (params: { [key: string]: any }) => {
          const time = params?.time || [];
          const startTime = time[0] && moment(time[0]).valueOf();
          const endTime = time[1] && moment(time[1]).valueOf();

          const data = await getHisAlarm({
            keyword: params.deviceName || undefined,
            number: params.pageSize,
            id: id || undefined,
            page: params.current,
            alarmGrade: params.alarmGrade,
            startTime: startTime || moment().startOf('day').subtract(7, 'day').valueOf(),
            endTime: endTime || moment().startOf('day').valueOf(),
          });
          return await {
            data: data?.alarmList || [],
            success: true,
            total: data?.count || 0,
          };
        }}
        rowKey="alarmID"
        className={styles['table-wrap']}
        tableAlertRender={false}
        formColConfig={{ md: 4, lg: 4, xl: 6, xxl: 6 }}
        actionRef={actionRef}
        dateFormatter="string"
        headerTitle={false}
        options={{
          search: {
            name: 'keyword',
          },
        }}
        toolBarRender={false}
      />
    </>
  );
};
export default LossCard;
